package com.wgl.service.impl;

import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wgl.db.pojo.Train;
import com.wgl.service.TrainService;
import com.wgl.utl.Tool;

@Service
public class TrainServiceImpl implements TrainService {

	@Override
	public String getTrainList(String fromStation, String toStation, String trainDate, String passengerType) {
		// TODO Auto-generated method stub
		LinkedHashMap<String, String> condition = new LinkedHashMap<String, String>();
		condition.put("leftTicketDTO.train_date", trainDate);
		condition.put("leftTicketDTO.from_station", fromStation);
		condition.put("leftTicketDTO.to_station", toStation);
		condition.put("purpose_codes", Tool.StringUtil.validStr(passengerType) ? passengerType : "ADULT");
		JSONObject result = new JSONObject();
		result.put("result", 0);
		List<Train> trainList = new ArrayList<Train>();
		try {
			trainList = Train.getTrainList(condition);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (trainList == null || trainList.isEmpty()) {
			return result.toJSONString();
		}
		JSONArray trainArray = new JSONArray();
		for (Train temp : trainList) {
			JSONObject trainInfo = new JSONObject();
			trainInfo.put("trainCode", temp.getTrainCode());
			trainInfo.put("hasTicket", temp.hasTicket());
			trainInfo.put("trainDate", temp.getTrainDate());
			trainInfo.put("totalTime", temp.getTotalTime());
			trainInfo.put("startTime", temp.getStartTime());
			trainInfo.put("endTime", temp.getEndTime());
			trainInfo.put("fromStation", temp.getFromStation());
			trainInfo.put("toStation", temp.getToStation());
			trainInfo.put("fromStationName", temp.getFromStationName());
			trainInfo.put("toStationName", temp.getToStationName());
			trainArray.add(trainInfo);
		}
		if (trainArray != null && trainArray.size() > 0) {
			result.put("result", trainArray.size());
			result.put("data", trainArray);
		}
		return result.toJSONString();
	}

	@Override
	public String addRemindTask(String trainDate, String trainCodeStr, String phoneNum) {
		// TODO Auto-generated method stub
		if (Tool.StringUtil.validStr(trainDate, trainCodeStr, phoneNum)) {
			String[] trainCode = trainCodeStr.split(",");
			for (String temp : trainCode) {
				if (Tool.StringUtil.validStr(temp)) {
				}
			}
		}
		JSONObject result = new JSONObject();
		result.put("result", 1);
		return result.toJSONString();
	}

}
